package com.ita;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.List;
import java.util.Properties;
import com.ita.command.Command;
import com.ita.dao.DepartmentDao;
import com.ita.dao.PeopleDao;
import com.ita.invoke.Invoke;
import com.ita.po.Department;
import com.ita.po.People;
import com.ita.util.Util;

public class Client {
	public static final String A_P = "A-P";
	public static final String A_D = "A-D";
	public static final String U_P = "U-P";
	public static final String U_D = "U-D";
	public static final String D_D = "D-D";
	public static final String D_P = "D-P";
	public static final String L_PA = "L-PA";
	public static final String L_DA = "L-DA";
	public static final String L_PD = "L-PD";
	public static final String FILEPATH = "command.properties";

	public static void main(String args[]) throws Exception {

		BufferedReader readerInConsole = new BufferedReader(new InputStreamReader(System.in));
		try {
			PeopleDao peopleDao = (PeopleDao) Naming.lookup("rmi://127.0.0.1:8888/peopleDao");
			DepartmentDao departmentDao = (DepartmentDao) Naming.lookup("rmi://127.0.0.1:8888/departmentDao");
			while (true) {
				Invoke invoke = null;
				String command = readerInConsole.readLine();
				if (command.equals("Q")) {
					System.out.println("bye");
					break;
				} else {
					execute(command, peopleDao, departmentDao, invoke);
				}
			}
		} catch (NotBoundException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

	private static String[] analyzeCmd(String str) {
		String[] splitStr = str.split(" ");
		return splitStr;
	}

	private static void execute(String command, PeopleDao peopleDao, DepartmentDao departmentDao, Invoke invoke)
			throws ParseException, RemoteException {
		Properties pps = new Properties();
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(FILEPATH));
			pps.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String[] cmdArr = analyzeCmd(command);
		String cmdStr = pps.getProperty(cmdArr[0]);
		Class<?> cmdClass = null;
		Command cmd = null;
		People people = null;
		Department department = null;
		if (cmdArr.length != 1) {
			if (cmdStr.endsWith("People")) {
				people = Util.strToPeople(cmdArr[1]);
			} else {
				department = Util.strToDepartment(cmdArr[1]);
			}
		}
		try {
			cmdClass = Class.forName(cmdStr);
			Constructor<?> cons[] = cmdClass.getConstructors();
			cmd = getCmd(cmdStr, cons[0], peopleDao, departmentDao, people, department);
			invoke = new Invoke(cmd);
			invoke.execute();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	private static Command getCmd(String str,Constructor<?> con,PeopleDao peopleDao,DepartmentDao departmentDao,People people,Department department) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		Command cmd = null;
		if (str.endsWith("People")) {
			if (str.endsWith("AllPeople")) {
				cmd = (Command) con.newInstance(peopleDao);
			}else{
				cmd = (Command) con.newInstance(peopleDao, people);
			}
		} else {
			if (str.endsWith("CmdAllDept")) {
				cmd = (Command) con.newInstance(departmentDao);
			}else{
				cmd = (Command) con.newInstance(departmentDao, department);
			}
		}
		return cmd;
	}
}
